package network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 10 - 06
 * Time: 15:54
 */
public class TcpEchoClient {
    private Socket socket = null;
    public TcpEchoClient(String serveIp, int servePort) throws IOException {
        socket = new Socket(serveIp, servePort);
    }
    public void start() {
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            while (true) {
                System.out.print("-> ");
                Scanner scanner = new Scanner(System.in);
                String request = scanner.next();
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.println(request);
                printWriter.flush();
                Scanner scannerNetwork = new Scanner(inputStream);
                String respon = new String(scannerNetwork.next());
                System.out.println(respon);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1", 9090);
        tcpEchoClient.start();
    }
}
